package yrq.leetcode.solution.acm;

import org.springframework.stereotype.Component;

/**
 * @author ：Excalibur
 * @create ：2020-09-03 16:05
 * @description：二分查找
 */
@Component
public class BinarySearch {
    
    public int example() {
        int[] ints = {70,80,90,1,2,3,4,5,6,7};
        int mid = bsearchWithoutRecursion(ints);
        return mid;
    }

    public int bsearchWithoutRecursion(int[] ints) {
        int lowIndex = 0;
        int highIndex = ints.length - 1;
        int key = ints[lowIndex];
        while (lowIndex <= highIndex) {
            int mid = lowIndex + (highIndex - lowIndex) / 2;
            int leftMid = mid+1;
            if (ints[leftMid] < ints[mid]) {
                return mid;
            } else if (ints[mid] > key) {
                lowIndex = mid + 1;
            } else if (ints[mid] < key) {
                highIndex = mid - 1;
            }
        }
        return -1;
    }
}
